﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinCourseControls.DAL;
using WinCourseControls.Models;

namespace WinCourseControls
{
    public partial class FrmCheckedListBox : Form
    {
        public FrmCheckedListBox()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择按钮-获取或设置选中状态值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelect_Click(object sender, EventArgs e)
        {
            chkList01.SetItemChecked(2, true);//将第3项选中
            chkList01.SetItemChecked(0, false);//将第1项取消选中
            chkList01.SetItemCheckState(1, CheckState.Checked);//将第2项设置为Checked
            string txt = chkList01.GetItemText("教师");//第4项的显示文本
            bool bl= chkList01.GetItemChecked(2);  // 获取复选框选中状态值
            CheckState ck = chkList01.GetItemCheckState(2);//复选框的状态的值  Checked  Unchecked  Indeterminate
        }

        /// <summary>
        /// CheckedListBox列表数据加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmCheckedListBox_Load(object sender, EventArgs e)
        {
            //绑定数据
            ClassDAL classDAL = new ClassDAL();
            List<ClassInfo> list = classDAL.GetModelList("IsDeleted=0", "", "");
            chkClasses.DataSource = list; // 绑定数据源
            chkClasses.DisplayMember = "ClassName"; // 显示文本
            chkClasses.ValueMember = "ClassId";  // 实际文本
            chkClasses.SelectedIndex = 0;  
        }

        /// <summary>
        /// 项勾选时触发的事件-状态将要更改时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkClasses_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            string txt = chkClasses.GetItemText(chkClasses.Items[e.Index]);//获取当前项的文本
            //if(chkClasses.GetItemChecked(e.Index))//发生前的状态，发生后取消勾选
            //{
            //    lbResults.Items.Remove(txt);
            //}
            //else
            //{
            //    lbResults.Items.Add(txt);
            //}
            if(e.NewValue==CheckState.Checked) // NewValue代表将要改变的值
            {
                lbResults.Items.Add(txt);
            }
            else
            {
                lbResults.Items.Remove(txt);
            }
        }

        /// <summary>
        /// 提交按钮-获取勾选的编号集合
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            List<int> listIds = new List<int>();
            // 方式一 通过控件集合方式
            //foreach(var item in chkClasses.CheckedItems) // 获取项
            //{
            //    ClassInfo info = item as ClassInfo;
            //    listIds.Add(info.ClassId);
            //}

            // 方式二 通过数据源方式
            List<ClassInfo> list = chkClasses.DataSource as List<ClassInfo>;
            foreach (int item in chkClasses.CheckedIndices) // 获取索引
            {
                listIds.Add(list[item].ClassId);  // 还是通过索引方式获取选中项
            }
        }
    }
}
